import {observable, action, runInAction} from 'mobx';
import {Modal} from 'antd-mobile';

const alert = Modal.alert;

/**
* mobx 状态管理类，所有的逻辑处理都在这里
* */
class ApplyListStore {
    @observable hasMore; //是否是最后一页
    @observable refreshing; //是否下拉刷新
    @observable isLoading; //是否上拉刷新
    @observable ownerData = []; //业主列表
    pageSize = 10;
    pageIndex = 1;

    @action
    onRefresh = () => {
        runInAction(() => {
            this.refreshing = true;
            this.isLoading = true;
        });
        this.pageIndex = 1;
        this.updateOwnerList(this.pageIndex);
    };

    @action
    onEndReached = () => {
        if (this.isLoading || this.hasMore) {
            return;
        }
        this.isLoading = true;
        this.pageIndex = ++this.pageIndex;
        this.updateOwnerList(this.pageIndex);

    };

    @action
    ownerOperation = (type, owner) => {
        let params = {ownerCode: owner.ownerCode};
        if (type === 1) {
            alert('通过提示', '确定要通过【' + owner.ownerName + '】成为业主吗？', [
                {text: '取消', onPress: () => console.log('cancel'), style: 'default'},
                {
                    text: '确定',
                    onPress: () => {
                        postJson("/owner/approveOwner.x", params, () => {
                            this.ownerData = this.ownerData.filter(x => x.ownerCode !== owner.ownerCode);
                        });
                    }
                },
            ]);
        } else if (type === 2) {
            alert('打回提示', '确定要打回【' + owner.ownerName + '】的申请吗？', [
                {text: '取消', onPress: () => console.log('cancel'), style: 'default'},
                {
                    text: '确定',
                    onPress: () => {
                        postJson("/owner/reject.x", params, () => {
                            this.ownerData = this.ownerData.filter(x => x.ownerCode !== owner.ownerCode);
                        });
                    }
                },
            ]);
        } else if (type === 3) {
            alert('删除提示', '确定要删除【' + owner.ownerName + '】的申请吗？', [
                {text: '取消', onPress: () => console.log('cancel'), style: 'default'},
                {
                    text: '确定',
                    onPress: () => {
                        postJson("/owner/remove.x", params, () => {
                            this.ownerData = this.ownerData.filter(x => x.ownerCode !== owner.ownerCode);
                        });
                    }
                },
            ]);
        }
    };

    updateOwnerList(pageIndex) {
        let params = {};
        params.page = pageIndex;
        params.pageSize = this.pageSize;
        params.applyType = 1;
        params.commCode = commCode;
        postJson("/owner/getOwnerList.x", params, (data) => {
            console.log("aaaaaaaaa------->", data);
            runInAction(() => {
                this.hasMore = data.list.length < 10;
                if (pageIndex === 1) {
                    this.ownerData = data.list;
                } else {
                    this.ownerData = this.ownerData.concat(data.list);
                }
                this.refreshing = false;
                this.isLoading = false;
            });
        }, (error) => {
            runInAction(() => {
                this.refreshing = false;
                this.isLoading = false;
            });
        });
    }
}

export default new ApplyListStore()